package com.mz.ieann.aide.widget.reward

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log

/**
 * <p>------------------------------------------------------
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> 点击开始，转盘不变，中间指针旋转绘制
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2023/3/10
 */
class RewardPointView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
): BaseRewardView(context, attrs, defStyle) {


    override fun drawStart(canvas: Canvas) {
        drawInsidePoint(canvas)
//        drawInsideCircle(canvas)
    }

    /**
     * 扇形区域平均分配角度
     */
    override fun drawArc(canvas: Canvas) {
        val count = describe?.size?: return
        val sweepAngle = (360 / count).toFloat()
        for (i in 0 until count) {
            val startAngle = if (i == 0) {
                revolveAngle - sweepAngle / 2
            } else {
                nextStartAngle
            }

            // 根据比例大小绘制扇形角度
            lotteryPaint.color = if (i % 2 == 0) Color.RED else Color.BLUE
            canvas.drawArc(
                outRectF,
                startAngle,
                sweepAngle,
                true,
                lotteryPaint
            )
            nextStartAngle = startAngle + sweepAngle

            // 居中绘制文字，扇形位置
            val des = describe?.get(i)?: "UNKNOW"
            val path = Path()
            path.addArc(outRectF, startAngle, sweepAngle)
            canvas.drawTextOnPath(
                des,
                path,
                // 文字横向偏移量
                0f,
                // 文字竖向偏移量
                radius / 8,
                textPaint
            )
            path.close()
        }
    }

    private val moveX by lazy { w + paddingStart }
    private val moveY by lazy { h + paddingTop }
    private var moveCount = 80F

    private val pointPath by lazy { Path() }

    /**
     * 绘制中间指针
     */
    override fun drawInsidePoint(canvas: Canvas) {
//        val pointPath = Path()

        Log.v(TAG, "----------------->> drawInsidePoint,moveCount:$moveCount")

        // canvas 清屏
        canvas.drawRect(0f, 0f, visibleWidth.toFloat(), visibleHeight.toFloat(), Paint().apply {
            xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
        })


        pointPath.moveTo(moveX - moveCount, moveY)
        pointPath.lineTo(moveX + moveCount, moveY)
        pointPath.lineTo(moveX, moveY - moveCount)
        pointPath.fillType = Path.FillType.WINDING
        canvas.drawPath(pointPath, pointPaint)
        pointPath.close()


        moveCount--

        if (moveCount < -80){
            moveCount = 80F
        }
    }

}